/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-04-15
 * Time: 12:07
 */
//376.摆动序列
class Solution2 {
    public int wiggleMaxLength(int[] nums) {
        int n = nums.length;
        //1.创建dp表
        //f[i]表示以i结尾的子序列最后一个位置呈现'上升'趋势的最长摆动序列
        //g[i]表示以i结尾的子序列最后一个位置呈现'下降'趋势的最长摆动序列
        int[] f = new int[n];
        int[] g = new int[n];
        //2.初始化
        for(int i = 0; i < n; i++) {
            f[i] = 1;
            g[i] = 1;
        }
        //3.状态转移
        int ret = 1;
        for(int i = 1; i < n; i++) {
            for(int j = i-1; j >= 0; j--) {
                if(nums[j] < nums[i]) {
                    f[i] = Math.max(f[i],g[j]+1);
                }
                if(nums[j] > nums[i]) {
                    g[i] = Math.max(g[i],f[j]+1);
                }
            }
            ret = Math.max(ret,Math.max(f[i],g[i]));
        }
        //4.填表顺序
        //5.返回值
        return ret;
    }
}
